package com.lwj.fenlispringboot.controller;

import com.lwj.fenlispringboot.entity.User;
import com.lwj.fenlispringboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //更新用户
    @PostMapping("/update")
    public Map<String,Object> update( @RequestBody User user){
        HashMap<String,Object> map=new HashMap<>();
        try {
            userService.update(user);
            map.put("success",true);
            map.put("msg","更新成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","更新用户失败"+e.getMessage());
        }
        return  map;
    }

    //查询一个用户信息
    @GetMapping("/findOne")
    public User findOne(String id){
        return userService.findById(id);
    }

    //删除
    @GetMapping("/delete")
    public Map<String,Object> delete( User user){
        HashMap<String,Object> map=new HashMap<>();
        try {
            userService.delete(user);
            map.put("success",true);
            map.put("msg","删除成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","删除用户失败"+e.getMessage());
        }
        return  map;
    }

    //添加
    @PostMapping("/add")
    public Map<String,Object> add( @RequestBody User user){
        HashMap<String,Object> map=new HashMap<>();
        try {
            userService.save(user);
            map.put("success",true);
            map.put("msg","添加成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","添加用户失败"+e.getMessage());
        }
        return  map;
    }

    //查询
    @GetMapping("/findAll")
    public Map<String,Object> findAll(Integer page,Integer rows){
        Map<String,Object> map=new HashMap<>();
        List<User> results= userService.findAll();
        map.put("total",10);
        map.put("totalPage",1);
        map.put("page",page);
        map.put("results",results);
        return map;
    }
}
